﻿Option Strict On
Option Infer On
Option Explicit On

Imports System
Imports System.Linq
Imports System.Xml.Linq
Imports System.Collections.Generic
Imports GenDotNet.StoredProcModel
Imports <xmlns:orm="http://kadgen.com/KADORM.xsd">


Public Class JoinData
   Inherits BaseData
   Implements IJoinData

   Private mJoinKeys As IEnumerable(Of IRelationKeyData)
   Private mJoinStyle As JoinStyle
   Private mJoinType As JoinType
   Private mParentAlias As String
   Private mParentTable As String
   Private mChildAlias As String
   Private mChildTable As String

   Public Shared Function Load(ByVal rootdata As RootData, ByVal elements As IEnumerable(Of XElement)) _
          As IEnumerable(Of IJoinData)
      Dim list = New List(Of IJoinData)
      If elements.Count = 0 Then
         Return list
      End If
      If elements(0).Name.LocalName = "ChildKey" Then
         Dim x = From element In elements _
                 Order By element.@TableName _
                 Group By ParentTableName = element.@ParentTable, _
                     ParentAlias = element.@ParentAlias, _
                     ChildTableName = element.@ChildTable, _
                     ChildAlias = element.@ChildAlias _
                 Into Group
         For Each tableGroup In x
            list.Add(New JoinData(rootdata, elements(0), tableGroup.ParentTableName, tableGroup.ParentAlias, tableGroup.ChildTableName, tableGroup.ChildAlias, tableGroup.Group))
         Next
      Else
         For Each e In elements
            list.Add(New JoinData(rootdata, e))
         Next
      End If
      Return list
   End Function

   ''' <summary>
   ''' This constructor is used with a normal join
   ''' </summary>
   ''' <param name="element">The Join element</param>
   Private Sub New( _
            ByVal rootdata As RootData, _
            ByVal element As XElement)
      MyBase.New(rootdata, element)
      Me.mJoinKeys = RelationKeyData.Load(rootdata, From e In element...<orm:JoinKey>)
      mJoinStyle = CType(System.Enum.Parse(GetType(JoinStyle), Me.Element.@JoinStyle), JoinStyle)
      mJoinType = ParseJoinType(Me.Element.@Type)
      mParentAlias = Me.Element.@LeftAlias
      mParentTable = Me.Element.@LeftOriginalName
      mChildAlias = Me.Element.@RightAlias
      mChildTable = Me.Element.@RightTable
   End Sub

   ''' <summary>
   ''' This constructor is used with the ChildKeys
   ''' </summary>
   ''' <param name="parentTableName">Name of the parent table</param>
   ''' <param name="parentAlias">Alias of the parent table</param>
   ''' <param name="childTableName">Name of the child table</param>
   ''' <param name="childAlias">Name of the alias table</param>
   ''' <param name="elements">ChildKey elements to build the JoinKey list</param>
   ''' <remarks></remarks>
   Private Sub New(ByVal rootdata As RootData, _
                   ByVal childKey As XElement, _
                   ByVal parentTableName As String, _
                   ByVal parentAlias As String, _
                   ByVal childTableName As String, _
                   ByVal childAlias As String, _
                   ByVal elements As IEnumerable(Of XElement))
      MyBase.New(rootdata, childKey)
      mJoinKeys = RelationKeyData.Load(rootdata, elements)
      mJoinStyle = StoredProcModel.JoinStyle.In
      mJoinType = StoredProcModel.JoinType.Inner
      mParentAlias = parentAlias
      mParentTable = parentTableName
      mChildAlias = childAlias
      mChildTable = childTableName
   End Sub

   Public ReadOnly Property JoinKeys() _
            As IEnumerable(Of IRelationKeyData) _
            Implements IJoinData.JoinKeys
      Get
         Return mJoinKeys
      End Get
   End Property

   Public ReadOnly Property JoinStyle() _
            As JoinStyle _
            Implements IJoinData.JoinStyle
      Get
         Return mJoinStyle
      End Get
   End Property

   Public ReadOnly Property JoinType() _
            As JoinType _
            Implements IJoinData.JoinType
      Get
         Return mJoinType
      End Get
   End Property

   Public ReadOnly Property ChildAlias() _
            As String _
            Implements IJoinData.ChildAlias
      Get
         Return mChildAlias
      End Get
   End Property


   Public ReadOnly Property ParentTable() _
            As StoredProcModel.ITableData _
            Implements StoredProcModel.IJoinData.ParentTable
      Get
         Return GetTable(mParentTable)
      End Get
   End Property

   Public ReadOnly Property ChildTable() _
            As StoredProcModel.ITableData _
            Implements StoredProcModel.IJoinData.ChildTable
      Get
         Return GetTable(mChildTable)
      End Get
   End Property

End Class
